#include <iostream>
#include <sstream>
#include <assert.h>
#include <websocketpp/config/asio.hpp>
#include <websocketpp/server.hpp>
// ----- -lssl -lcrypto   ----------------
void test_construct_server_asio_tls (){
    websocketpp::server<websocketpp::config::asio_tls> s;
    s.init_asio();
}

void test_test_construct_server_asio1(){
    websocketpp::server<websocketpp::config::asio> s;
    s.init_asio();
}
void test_test_construct_server_asio2(){
    websocketpp::server<websocketpp::config::asio> s;
    boost::asio::io_service ios;
    s.init_asio(&ios);
}

void test_move_construct_server_core(){
    websocketpp::server<websocketpp::config::core> s1;
    websocketpp::server<websocketpp::config::core> s2(std::move(s1));
}


void test_listen_after_listen_failure (){
    using websocketpp::transport::asio::error::make_error_code;
    using websocketpp::transport::asio::error::pass_through;

    websocketpp::server<websocketpp::config::asio> server1;
    websocketpp::server<websocketpp::config::asio> server2;

    websocketpp::lib::error_code ec;

    server1.init_asio();
    server2.init_asio();

    boost::asio::ip::tcp::endpoint ep1(boost::asio::ip::address::from_string("127.0.0.1"), 12345);
    boost::asio::ip::tcp::endpoint ep2(boost::asio::ip::address::from_string("127.0.0.1"), 23456);

    server1.listen(ep1, ec);
    assert(!ec);

    server2.listen(ep1, ec);  // error: [2021-02-04 10:40:35] [info] asio listen error: system:98 (Address already in use)
    assert(ec);


    server2.listen(ep2, ec);
    assert(!ec);
}



struct endpoint_extension{
    endpoint_extension() : extension_value(5) {}

    int extension_method() {
        return extension_value;
    }

    bool is_server() const {
        return false;
    }

    int extension_value;
};
struct stub_config : public websocketpp::config::core {
    typedef core::concurrency_type concurrency_type;

    typedef core::request_type request_type;
    typedef core::response_type response_type;

    typedef core::message_type message_type;
    typedef core::con_msg_manager_type con_msg_manager_type;
    typedef core::endpoint_msg_manager_type endpoint_msg_manager_type;

    typedef core::alog_type alog_type;
    typedef core::elog_type elog_type;

    typedef core::rng_type rng_type;

    typedef core::transport_type transport_type;

    typedef endpoint_extension endpoint_base;
};

void test_endpoint_extensions (){
    websocketpp::server<stub_config> s;

    assert(s.extension_value == 5);
    assert(s.extension_method() == 5);

    assert(s.is_server());
}
int main(){
    test_construct_server_asio_tls();
    test_test_construct_server_asio1();
    test_test_construct_server_asio2();
    test_move_construct_server_core();
    test_listen_after_listen_failure();

}
